#include "mytreeview.h"
#include <QHeaderView>
#include <QPoint>
MyTreeView::MyTreeView(QWidget *parent)
    : QTreeView{parent}
    , m_backGroundColor("#1C1C24")
    , m_nID(0)
{
    header()->setVisible(false);
    m_model.setColumnCount(1);
    setModel(&m_model);
    m_delegate = new MyTreeItemDelegate(10, this);
    setItemDelegate(m_delegate);

    //connect(this, SIGNAL(clicked(QModelIndex)), this, SLOT(slotTreeItemClick(QModelIndex)));
    //setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
}

QStandardItem * MyTreeView::addItem(stIemData ItemData)
{
    QStandardItem *rootItem = new QStandardItem(ItemData.m_qsMessage);
    ItemData.m_lID = m_nID;
    m_nID++;
    rootItem->setData(QVariant::fromValue(ItemData), ItemRoles::IdRole);
    m_model.appendRow(rootItem);
    m_delegate->addItem(ItemData.m_lID, ItemData.m_qsImageUrl, ItemData.m_qsHilightUrl);
    // 禁止编辑 childItem1
    rootItem->setFlags(rootItem->flags() & ~Qt::ItemIsEditable);
    return rootItem;
}

QStandardItem *MyTreeView::addItem(QStandardItem * parentItem, stIemData ItemData)
{
    QStandardItem *rootItem = new QStandardItem(ItemData.m_qsMessage);
    ItemData.m_lID = m_nID;
    m_nID++;
    rootItem->setData(QVariant::fromValue(ItemData), ItemRoles::IdRole);
    parentItem->appendRow(rootItem);
    m_delegate->addItem(ItemData.m_lID, ItemData.m_qsImageUrl, ItemData.m_qsHilightUrl);
    // 禁止编辑 childItem1
    rootItem->setFlags(rootItem->flags() & ~Qt::ItemIsEditable);
    return rootItem;
}

void MyTreeView::setBackgroundColor(QString qsColor)
{
    setStyleSheet(
        "QTreeView {"
        "    background-color: #1C1C24;"    // 设置整个 QTreeView 的背景色
        "    border: 0px solid #1C1C24;"
        "}"
        );
}

QStandardItem *MyTreeView::getItem(QModelIndex index)
{
    return m_model.itemFromIndex(index);
}

void MyTreeView::slotTreeItemClick(QModelIndex index)
{
    //QStandardItem *item = m_model->itemFromIndex(index);
}


